var locationpath = "root"; // 记录当前文件路径
var parentpath = "null"; // 记录当前文件路径的父级目录
var ap; // 音乐播放器对象
var zipTimer; // 打包下载计时器
var folderView; // 返回的文件系统视图对象
var originFolderView; // 保存原始的文件视图对象
var fs; // 选中的要上传的文件列表
var checkedMovefiles; // 移动文件的存储列表
var constraintLevel; // 当前文件夹限制等级
var account; // 用户账户
var isUpLoading = false; // 是否正在执行其他上传操作
var xhr; // 文件上传请求对象
// 界面功能方法定义
// 页面初始化
$(function() {
	window.onresize = function() {
		changeFilesTableStyle();
	}
	showFolderView("root"); // 显示根节点页面视图
	// 点击空白处取消选中文件（已尝试兼容火狐，请期待用户反馈，如不好使再改）
	$(document).click(function(e) {
		var filetable = $("#filetable")[0];
		var srcElement = e.srcElement;
		if (!srcElement) {
			srcElement = e.target;
		}
		if (srcElement !== filetable && !$.contains(filetable, e.target)) {
			$(".filerow").removeClass("info");
		}
	});
	// 关闭打包下载模态框自动停止计时
	$('#downloadAllCheckedModal').on('hidden.bs.modal', function(e) {
		if (zipTimer != null) {
			window.clearInterval(zipTimer);
		}
	});

	// 各个模态框的打开判定及回车响应功能。该功能仅对“首选”的按钮有效，对其他按钮无效，以避免用户误操作。
	$('.modal').on('shown.bs.modal', function(e) {
		$(this).addClass("shown");
	});
	$('.modal').on('hidden.bs.modal', function(e) {
		$(this).removeClass("shown");
	});
	$("body").keypress(function(e) {
		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		if (keyCode == 13) {
			if ("sreachKeyWordIn" === document.activeElement.id) {
				doSearchFile();
			} else {
				var g = $(".shown .btn-primary");
				if (g.get(0) != null) {
					g.click();
				}
			}
			return false;
		}
	});

	// 开启新建文件夹框自动初始化状态
	$('#newFolderModal')
			.on(
					'show.bs.modal',
					function(e) {
						$("#folderalert").removeClass("alert");
						$("#folderalert").removeClass("alert-danger");
						$("#foldernamebox").removeClass("has-error");
						$("#folderalert").text("");
						$("#foldername").val("");
						$("#foldertypelist").html("");
						if (account != null) {
							$("#foldername").attr("folderConstraintLevel",
									constraintLevel + "");
							$("#newfoldertype").text(
									folderTypes[constraintLevel]);
							for (var i = constraintLevel; i < folderTypes.length; i++) {
								$("#foldertypelist").append(
										"<li><a onclick='changeNewFolderType("
												+ i + ")'>" + folderTypes[i]
												+ "</a></li>");
							}
						} else {
							$("#foldertypelist").append(
									"<li><a onclick='changeNewFolderType(0)'>"
											+ folderTypes[0] + "</a></li>");
						}
					});
	// 开启新建文件夹模态框自动聚焦文件名输入框
	$('#newFolderModal').on('shown.bs.modal', function(e) {
		$("#foldername").focus();
	});
	// 关闭上传模态框时自动提示如何查看上传进度
	$('#uploadFileModal').on('hidden.bs.modal', function(e) {
		if (isUpLoading) {
			$('#operationMenuBox').attr("data-placement", "top");
			$('#operationMenuBox').attr("data-trigger", "focus");
			$('#operationMenuBox').attr("data-title", "上传中");
			$('#operationMenuBox').attr("data-content", "您可以重新打开上传窗口查看上传进度。");
			$('#operationMenuBox').popover();
			$('#operationMenuBox').popover('show');
			// 2秒后消失提示框
			var closeUploadTips = setTimeout(function() {
				$('#operationMenuBox').attr("data-title", "");
				$('#operationMenuBox').attr("data-content", "");
				$('#operationMenuBox').popover('destroy');
			}, 2000);
		}
	});
	// 开启编辑文件夹框自动初始化状态
	$('#renameFolderModal')
			.on(
					'show.bs.modal',
					function(e) {
						$("#newfolderalert").removeClass("alert");
						$("#newfolderalert").removeClass("alert-danger");
						$("#folderrenamebox").removeClass("has-error");
						$("#newfolderalert").text("");
						$("#editfoldertypelist").html("");
						if (account != null) {
							for (var i = constraintLevel; i < folderTypes.length; i++) {
								$("#editfoldertypelist").append(
										"<li><a onclick='changeEditFolderType("
												+ i + ")'>" + folderTypes[i]
												+ "</a></li>");
							}
						} else {
							$("#editfoldertypelist").append(
									"<li><a onclick='changeEditFolderType(0)'>"
											+ folderTypes[0] + "</a></li>");
						}
					});
	// 响应拖动上传文件
	document.ondragover = function(e) {
		if (e.preventDefault) {
			e.preventDefault();
			e.stopPropagation();
		} else {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
	}
	document.ondrop = function(e) {
		if (e.preventDefault) {
			e.preventDefault();
			e.stopPropagation();
		} else {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (folderView.authList != null) {
			if (checkAuth(folderView.authList, "U")) { // 如果有上传权限且未进行其他上传
				if (isUpLoading) {
					alert("提示：您正在执行另一项上传任务，请在上传窗口关闭后再试。");
				} else {
					if (!(window.ActiveXObject || "ActiveXObject" in window)) { // 判断是否为IE
						var dt;
						if (e.dataTransfer != null) {
							dt = e.dataTransfer; // 获取到拖入上传的文件对象
						} else {
							dt = window.event.dataTransfer;
						}
						var testFile = true;
						if (dt.items !== undefined) {
							for (var i = 0; i < dt.items.length; i++) {
								var item = dt.items[i];
								if (item.kind === "file"
										&& item.webkitGetAsEntry().isFile) {

								} else {
									testFile = false;
								}
							}
						} else {
							for (var i = 0; i < dt.files.length; i++) {
								var dropFile = df.files[i];
								if (dropFile.type) {

								} else {
									try {
										var fileReader = new FileReader();
										fileReader.readAsDataURL(dropFile
												.slice(0, 10));
										fileReader.addEventListener('load',
												function(e) {

												}, false);
										fileReader.addEventListener('error',
												function(e) {
													testFile = false;
												}, false);
									} catch (e) {
										testFile = false;
									}
								}
							}
						}
						if (testFile) {
							fs = e.dataTransfer.files; // 获取到拖入上传的文件对象
							showUploadFileModel();
							showfilepath();
							checkUploadFile();
						} else {
							alert("提示：您拖入的文件中包含了一个或多个文件夹，无法进行上传。");
						}
					} else {
						alert("提示：IE浏览器不支持拖拽上传。您可以使用现代浏览器或将浏览模式切换为“极速模式”来体验该功能。");
					}
				}
			} else {
				alert("提示：您不具备上传权限，无法上传文件。");
			}
		} else {
			alert("提示：您不具备上传权限，无法上传文件。");
		}
	}
	// Shift+A全选文件/反选文件，Shift+N新建文件夹，Shift+U上传文件，Shift+C&V剪切粘贴，Shift+D批量删除
	$(document).keypress(
			function(e) {
				if ($('.modal.shown').length == 0
						|| ($('.modal.shown').length == 1 && $('.modal.shown')
								.attr('id') == 'loadingModal')) {
					var keyCode = e.keyCode ? e.keyCode : e.which ? e.which
							: e.charCode;
					if (isShift(e)) {
						switch (keyCode) {
						case 65:
							checkallfile();
							break;
						case 78:
							$('#createFolderButtonLi a').click();
							break;
						case 85:
							$('#uploadFileButtonLi a').click();
							break;
						case 68:
							$('#deleteSeelectFileButtonLi a').click();
							break;
						case 67:
							if ((!$("#cutSignTx").hasClass("cuted"))
									&& checkedMovefiles == undefined) {
								$('#cutFileButtonLi a').click();
							}
							break;
						case 86:
							if ($("#cutSignTx").hasClass("cuted")
									&& checkedMovefiles !== undefined) {
								$('#cutFileButtonLi a').click();
							}
							break;

						default:
							break;
						}
						return false;
					}
				}
			});
	// 关闭移动提示框自动取消移动
	$('#moveFilesModal')
			.on(
					'hidden.bs.modal',
					function(e) {
						checkedMovefiles = undefined;
						$("#cutSignTx")
								.html(
										"剪切 <span class='pull-right'><span class='glyphicon glyphicon-arrow-up' aria-hidden='true'></span>+C</span>");
						$("#cutSignTx").removeClass("cuted");
						$('#moveFilesBox').html("");
					});
	// IE内核浏览器内的startsWith方法的自实现
	if (typeof String.prototype.startsWith != 'function') {
		String.prototype.startsWith = function(prefix) {
			return this.slice(0, prefix.length) === prefix;
		};
	}
	if (typeof String.prototype.endsWith != 'function') {
		String.prototype.endsWith = function(suffix) {
			return this.indexOf(suffix, this.length - suffix.length) !== -1;
		};
	}
	// 开启详细信息模态框自动显示信息内容
	$('#folderInfoModal').on(
			'show.bs.modal',
			function(e) {
				var f = folderView.folder;
				$("#fim_name").text(f.folderName);
				$("#fim_creator").text(f.folderCreator);
				$("#fim_folderCreationDate").text(f.folderCreationDate);
				$("#fim_statistics").text(
						"共包含 " + folderView.folderList.length + " 个文件夹， "
								+ folderView.fileList.length + " 个文件。");
			});
	// 关闭下载提示模态框自动隐藏下载链接
	$('#downloadModal').on('hidden.bs.modal', function(e) {
		$('#downloadURLCollapse').collapse('hide');
	});
});
// 全局请求失败提示
function doAlert() {
	alert("错误：无法连接到文件管理服务器，请检查您的网络连接或查看服务器运行状态。");
}
// 根据屏幕大小增删表格显示内容
function changeFilesTableStyle() {
	var win = $(window).width();
	if (win < 800) {
		$('#filetableheadera').addClass('filetableheaderstyle');
		$('.hiddenColumn').hide();
		$('.rightbtn').hide();
		$('#vicetbbox').show();
		$('#filetableoptmenusreach').hide();
	} else {
		$('#filetableheadera').removeClass('filetableheaderstyle');
		$('.hiddenColumn').show();
		$('.rightbtn').show();
		$('#vicetbbox').hide();
		$('#filetableoptmenusreach').show();
	}
	if (win < 768) {
		$('#filetableheadera').attr('data-toggle', 'collapse');
		$('#filetableheadera').attr('data-target', '#filetableoptmenu');
		$('#mdropdownicon').html('（点击展开/折叠菜单）');
	} else {
		$('#filetableheadera').attr('data-toggle', 'modal');
		$('#filetableheadera').attr('data-target', '#folderInfoModal');
		$('#mdropdownicon').html('');
	}
}
// 获取实时文件夹视图
function showFolderView(fid) {
	startLoading();
	$.ajax({
		type : 'POST',
		dataType : 'text',
		data : {
			fid : fid
		},
		url : '/filemgrctl/getFolderView.ajax',
		success : function(result) {
			endLoading();
			folderView = eval("(" + result + ")");
			locationpath = folderView.folder.folderId;
			parentpath = folderView.folder.folderParent;
			constraintLevel = folderView.folder.folderConstraint;
			screenedFoldrView = null;
			$("#sreachKeyWordIn").val("");
			showParentList(folderView);
			showAccountView(folderView);
			showPublishTime(folderView);
			originFolderView = $.extend(true, {}, folderView);
			$("#sortByFN").removeClass();
			$("#sortByCD").removeClass();
			$("#sortByFS").removeClass();
			$("#sortByCN").removeClass();
			showFolderTable(folderView);
		},
		error : function() {
			endLoading();
			doAlert();
			$("#tb").html("<span class='graytext'>获取失败，请尝试刷新</span>");
			$("#publishTime").html("<span class='graytext'>获取失败，请尝试刷新</span>");
			$("#parentlistbox")
					.html("<span class='graytext'>获取失败，请尝试刷新</span>");
		}
	});
}
// 开始文件视图加载动画
function startLoading() {
	$('#loadingModal').modal({
		backdrop : 'static',
		keyboard : false
	});
	$('#loadingModal').modal('show');
	$('#loadingModal').addClass("shown");
}

// 结束文件视图加载动画
function endLoading() {
	$('#loadingModal').modal('hide');
	$('#loadingModal').removeClass("shown");
}
// 显示当前文件夹的父级路径
function showParentList(folderView) {
	$("#parentFolderList").html("");
	var f = folderView.folder;
	if (folderView.parentList.length > 0) {
		$.each(folderView.parentList, function(n, val) {
			$("#parentFolderList").append(
					"<li><a href='javascript:void(0);' onclick='entryFolder("
							+ '"' + val.folderId + '"' + ")'>" + val.folderName
							+ "</a></li>");
		});
	} else {
		$("#parentFolderList").html("<li class='disabled'><a>无</a></li>");
	}
	if (f.folderName.length > 6) {
		$("#currentFolderName").text(f.folderName.substr(0, 6) + "...");
	} else {
		$("#currentFolderName").text(f.folderName);
	}
	if (f.folderName == "ROOT") {
		$("#folderIconSpan").removeClass("glyphicon-folder-close");
		$("#folderIconSpan").addClass("glyphicon-home");
	} else {
		$("#folderIconSpan").removeClass("glyphicon-home");
		$("#folderIconSpan").addClass("glyphicon-folder-close");
	}
}

// 显示用户视图，包括文件列表、登录信息、操作权限接口等
function showAccountView(folderView) {
	$("#tb,#tb2").html("");
	account = folderView.account;
	$("#tb")
			.append(
					"<button class='btn btn-link rightbtn' data-toggle='modal' data-target='#logoutModal'>当前用户 ["
							+ folderView.account
							+ "] </button>");
	$("#tb2")
			.append(
					"<button class='btn btn-link' data-toggle='modal' data-target='#logoutModal'>当前用户 ["
							+ folderView.account
							+ "] </button>");

	var authList = folderView.authList;
	// 对操作菜单进行初始化，根据权限显示可操作的按钮（并非约束）。
	$("#fileListDropDown li").addClass("disabled");
	$("#fileListDropDown li a").attr("onclick", "");
	$("#fileListDropDown li a").attr("href", "javascript:void(0);");
	if (authList != null) {
		if (checkAuth(authList, "C")) {
			$("#createFolderButtonLi").removeClass("disabled");
			$("#createFolderButtonLi a")
					.attr("onclick", "showNewFolderModel()");
		}
		if (checkAuth(authList, "U")) {
			$("#uploadFileButtonLi").removeClass("disabled");
			$("#uploadFileButtonLi a").attr("onclick", "showUploadFileModel()");
		}
		if (checkAuth(authList, "L")) {
			$("#packageDownloadBox")
					.html(
							"<button class='btn btn-link navbar-btn' onclick='showDownloadAllCheckedModel()'><span class='glyphicon glyphicon-briefcase'></span> 打包下载</button>");
		} else {
			$("#packageDownloadBox").html("");
		}
		if (checkAuth(authList, "D")) {
			$("#deleteSeelectFileButtonLi").removeClass("disabled");
			$("#deleteSeelectFileButtonLi a").attr("onclick",
					"showDeleteAllCheckedModel()");
		}
		if (checkAuth(authList, "M")) {
			$("#cutFileButtonLi").removeClass("disabled");
			$("#cutFileButtonLi a").attr("onclick", "startMoveFile()");
			if (checkedMovefiles !== undefined && checkedMovefiles.length > 0) {
				$("#cutSignTx").text("粘贴（" + checkedMovefiles.length + "）");
				$("#cutSignTx").addClass("cuted");
			}
		}
	}
}

// 检查权限列表
function checkAuth(authList, auth) {
	var k = false;
	$.each(authList, function(n, a) {
		if (a == auth) {
			k = true;
		}
	});
	return k;
}

// 显示视图更新时间
function showPublishTime(folderView) {
	$("#publishTime").html("");
	var pt = "";
	if (folderView.publishTime != null) {
		pt = folderView.publishTime;
	} else {
		pt = "--";
	}
	$("#publishTime").text(pt);
}

// 刷新文件夹视图
function refreshFolderView() {
	if (locationpath != null && locationpath.length > 0) {
		showFolderView(locationpath);
	} else {
		showFolderView('root');
	}
}

// 返回上一级文件夹
function returnPF() {
	if (parentpath != null && parentpath != "null") {
		showFolderView(parentpath);
	} else {
		showFolderView('root');
	}
}

// 显示文件夹内容
function showFolderTable(folderView) {
	$("#foldertable").html("");
	if (parentpath != null && parentpath != "null") {
		$("#foldertable")
				.append(
						"<tr onclick='returnPF()'><td><button onclick='returnPF()' class='btn btn-link btn-xs'>../</button></td><td class='hiddenColumn'>--</td><td>--</td><td class='hiddenColumn'>--</td><td>--</td></tr>");
	}
	var authList = folderView.authList;
	var aD = false;
	var aR = false;
	var aL = false;
	if (checkAuth(authList, "D")) {
		aD = true;
	}
	if (checkAuth(authList, "R")) {
		aR = true;
	}
	if (checkAuth(authList, "L")) {
		aL = true;
	}
	$
			.each(
					folderView.folderList,
					function(n, f) {
						var folderRow = "<tr id='"
								+ f.folderId
								+ "' onclick='checkfile(event,"
								+ '"'
								+ f.folderId
								+ '"'
								+ ")' ondblclick='checkConsFile(event,"
								+ '"'
								+ f.folderId
								+ '"'
								+ ")' class='filerow' iskfolder='true' ><td><button onclick='entryFolder("
								+ '"' + f.folderId + '"'
								+ ")' class='btn btn-link btn-xs'>/"
								+ f.folderName
								+ "</button></td><td class='hiddenColumn'>"
								+ f.folderCreationDate
								+ "</td><td>--</td><td class='hiddenColumn'>"
								+ f.folderCreator + "</td><td>";
						if (aD) {
							folderRow = folderRow
									+ "<button onclick='showDeleteFolderModel("
									+ '"'
									+ f.folderId
									+ '","'
									+ f.folderName
									+ '"'
									+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-remove'></span> 删除</button>";
						}
						if (aR) {
							folderRow = folderRow
									+ "<button onclick='showRenameFolderModel("
									+ '"'
									+ f.folderId
									+ '","'
									+ f.folderName
									+ '",'
									+ f.folderConstraint
									+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-wrench'></span> 编辑</button>";
						}
						if (!aR && !aD) {
							folderRow = folderRow + "--";
						}
						folderRow = folderRow + "</td></tr>";
						$("#foldertable").append(folderRow);
					});
	$
			.each(
					folderView.fileList,
					function(n, fi) {
						var fileRow = "<tr onclick='checkfile(event," + '"'
								+ fi.fileId + '"'
								+ ")' ondblclick='checkConsFile(event," + '"'
								+ fi.fileId + '"' + ")' id='" + fi.fileId
								+ "' class='filerow'><td>" + fi.fileName
								+ "</td><td class='hiddenColumn'>"
								+ fi.fileCreationDate + "</td>";
						if (fi.fileSize == "0") {
							fileRow = fileRow + "<td>&lt;1MB</td>";
						} else {
							fileRow = fileRow + "<td>" + fi.fileSize
									+ "MB</td>";
						}
						fileRow = fileRow + "<td class='hiddenColumn'>"
								+ fi.fileCreator + "</td><td>";
						if (aL) {
							fileRow = fileRow
									+ "<button onclick='showDownloadModel("
									+ '"'
									+ fi.fileId
									+ '","'
									+ fi.fileName
									+ '"'
									+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-cloud-download'></span> 下载</button>";
							// 对于各种特殊格式文件提供的预览和播放功能
							if (getSuffix(fi.fileName) == "mp4"
									|| getSuffix(fi.fileName) == "webm"
									|| getSuffix(fi.fileName) == "mov") {
								fileRow = fileRow
										+ "<button onclick='playVideo("
										+ '"'
										+ fi.fileId
										+ '"'
										+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-play'></span> 播放</button>";
							} else if (getSuffix(fi.fileName) == "pdf") {
								fileRow = fileRow
										+ "<button onclick='pdfView("
										+ '"'
										+ fi.filePath
										+ '"'
										+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-eye-open'></span> 预览</button>";
							} else if (getSuffix(fi.fileName) == "jpg"
									|| getSuffix(fi.fileName) == "jpeg"
									|| getSuffix(fi.fileName) == "gif"
									|| getSuffix(fi.fileName) == "png"
									|| getSuffix(fi.fileName) == "bmp") {
								fileRow = fileRow
										+ "<button onclick='showPicture("
										+ '"'
										+ fi.fileId
										+ '"'
										+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-picture'></span> 查看</button>";
							} else if (getSuffix(fi.fileName) == "mp3"
									|| getSuffix(fi.fileName) == "wav"
									|| getSuffix(fi.fileName) == "ogg") {
								fileRow = fileRow
										+ "<button onclick='playAudio("
										+ '"'
										+ fi.fileId
										+ '"'
										+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-play'></span> 播放</button>";
							}
						}
						if (aD) {
							fileRow = fileRow
									+ "<button onclick='showDeleteFileModel("
									+ '"'
									+ fi.fileId
									+ '","'
									+ fi.fileName
									+ '"'
									+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-remove'></span> 删除</button>";
						}
						if (aR) {
							fileRow = fileRow
									+ "<button onclick='showRenameFileModel("
									+ '"'
									+ fi.fileId
									+ '"'
									+ ","
									+ '"'
									+ fi.fileName
									+ '"'
									+ ")' class='btn btn-link btn-xs'><span class='glyphicon glyphicon-wrench'></span> 重命名</button>";
						}
						if (!aR && !aD && !aL) {
							fileRow = fileRow + "--";
						}
						fileRow = fileRow + "</td></tr>";
						$("#foldertable").append(fileRow);
					});
	changeFilesTableStyle();
}

var folderTypes = [ '公开的', '仅小组', '仅创建者' ]; // 文件夹约束条件（由小至大）

// 显示新建文件夹模态框
function showNewFolderModel() {
	$('#newFolderModal').modal('show');
}

// 修改新建文件夹约束等级
function changeNewFolderType(type) {
	$("#newfoldertype").text(folderTypes[type]);
	$("#foldername").attr("folderConstraintLevel", type + "");
}

// 创建新的文件夹
function createfolder() {
	var fn = $("#foldername").val();
	var fc = $("#foldername").attr("folderConstraintLevel");
	var reg = new RegExp("^[0-9a-zA-Z_\\u4E00-\\u9FFF]+$", "g");
	if (fn.length == 0) {
		showFolderAlert("提示：文件夹名称不能为空。");
	} else if (fn.length > 20) {
		showFolderAlert("提示：文件夹名称太长。");
	} else if (reg.test(fn)) {
		$("#folderalert").removeClass("alert");
		$("#folderalert").removeClass("alert-danger");
		$("#foldernamebox").removeClass("has-error");
		$("#folderalert").text("");
		$.ajax({
			type : "POST",
			dataType : "text",
			data : {
				parentId : locationpath,
				folderName : fn,
				folderConstraint : fc
			},
			url : "/filemgrctl/newFolder.ajax",
			success : function(result) {
				if (result == "mustLogin") {
					window.location.href = "login.html";
				} else {
					if (result == "noAuthorized") {
						showFolderAlert("提示：您的操作未被授权，创建文件夹失败。");
					} else if (result == "errorParameter") {
						showFolderAlert("提示：参数不正确，创建文件夹失败。");
					} else if (result == "cannotCreateFolder") {
						showFolderAlert("提示：出现意外错误，可能未能创建文件夹。");
					} else if (result == "nameOccupied") {
						showFolderAlert("提示：该名称已被占用，请选取其他名称。");
					} else if (result == "createFolderSuccess") {
						$('#newFolderModal').modal('hide');
						showFolderView(locationpath);
					} else {
						$('#newFolderModal').modal('hide');
						showFolderView(locationpath);
					}
				}
			},
			error : function() {
				showFolderAlert("提示：出现意外错误，可能未能创建文件夹");
			}
		});
	} else {
		showFolderAlert("提示：文件夹名只能包含英文字母、数组、汉字和下划线。");
	}
}

// 显示新建文件夹状态提示
function showFolderAlert(txt) {
	$("#folderalert").addClass("alert");
	$("#folderalert").addClass("alert-danger");
	$("#foldernamebox").addClass("has-error");
	$("#folderalert").text(txt);
}

// 进入某一文件夹
function entryFolder(folderId) {
	showFolderView(folderId);
}

// 显示删除文件夹模态框
function showDeleteFolderModel(folderId, folderName) {
	$('#deleteFolderBox')
			.html(
					"<button id='dmbutton' type='button' class='btn btn-danger' onclick='deleteFolder("
							+ '"' + folderId + '"' + ")'>删除</button>");
	$("#dmbutton").attr('disabled', false);
	$('#deleteFolderMessage').text(
			"提示：确定要彻底删除文件夹：[" + folderName + "]及其全部内容么？该操作不可恢复");
	$('#deleteFolderModal').modal('toggle');
}

// 执行删除文件夹
function deleteFolder(folderId) {
	$("#dmbutton").attr('disabled', true);
	$('#deleteFolderMessage').text("提示：正在删除，请稍候...");
	$.ajax({
		type : "POST",
		dataType : "text",
		data : {
			folderId : folderId
		},
		url : "/filemgrctl/deleteFolder.ajax",
		success : function(result) {
			if (result == "mustLogin") {
				window.location.href = "login.html";
			} else {
				if (result == "noAuthorized") {
					$('#deleteFolderMessage').text("提示：您的操作未被授权，删除文件夹失败");
					$("#dmbutton").attr('disabled', false);
				} else if (result == "errorParameter") {
					$('#deleteFolderMessage').text("提示：参数不正确，删除文件夹失败");
					$("#dmbutton").attr('disabled', false);
				} else if (result == "cannotDeleteFolder") {
					$('#deleteFolderMessage').text("提示：出现意外错误，可能未能删除文件夹");
					$("#dmbutton").attr('disabled', false);
				} else if (result == "deleteFolderSuccess") {
					$('#deleteFolderModal').modal('hide');
					showFolderView(locationpath);
				} else {
					$('#deleteFolderMessage').text("提示：出现意外错误，可能未能删除文件夹");
					$("#dmbutton").attr('disabled', false);
				}
			}
		},
		error : function() {
			$('#deleteFolderMessage').text("提示：出现意外错误，可能未能删除文件夹");
			$("#dmbutton").attr('disabled', false);
		}
	});
}

// 显示重命名文件夹模态框
function showRenameFolderModel(folderId, folderName, type) {
	$("#renameFolderBox").html(
			"<button type='button' class='btn btn-primary' onclick='renameFolder("
					+ '"' + folderId + '"' + ")'>修改</button>");
	$("#newfoldername").val(folderName);
	changeEditFolderType(type);
	$("#renameFolderModal").modal('show');
}

// 修改编辑文件夹的约束等级
function changeEditFolderType(type) {
	$("#editfoldertype").text(folderTypes[type]);
	$("#newfoldername").attr("folderConstraintLevel", type + "");
}

// 执行重命名文件夹
function renameFolder(folderId) {
	var newName = $("#newfoldername").val();
	var fc = $("#newfoldername").attr("folderConstraintLevel");
	var reg = new RegExp("^[0-9a-zA-Z_\\u4E00-\\u9FFF]+$", "g");
	if (newName.length == 0) {
		showRFolderAlert("提示：文件夹名称不能为空。");
	} else if (newName.length > 20) {
		showRFolderAlert("提示：文件夹名称太长。");
	} else if (reg.test(newName)) {
		$("#newfolderalert").removeClass("alert");
		$("#newfolderalert").removeClass("alert-danger");
		$("#folderrenamebox").removeClass("has-error");
		$("#newfolderalert").text("");
		$.ajax({
			type : "POST",
			dataType : "text",
			data : {
				folderId : folderId,
				newName : newName,
				folderConstraint : fc
			},
			url : "/filemgrctl/renameFolder.ajax",
			success : function(result) {
				if (result == "mustLogin") {
					window.location.href = "login.html";
				} else {
					if (result == "noAuthorized") {
						showRFolderAlert("提示：您的操作未被授权，编辑失败。");
					} else if (result == "errorParameter") {
						showRFolderAlert("提示：参数不正确，编辑失败，请刷新后重试。");
					} else if (result == "nameOccupied") {
						showRFolderAlert("提示：该名称已被占用，请选取其他名称。");
					} else if (result == "renameFolderSuccess") {
						$('#renameFolderModal').modal('hide');
						showFolderView(locationpath);
					} else {
						showRFolderAlert("提示：出现意外错误，可能未能编辑文件夹，请刷新后重试。");
					}
				}
			},
			error : function() {
				showRFolderAlert("提示：出现意外错误，可能未能编辑文件夹，请刷新后重试。");
			}
		});
	} else {
		showRFolderAlert("提示：文件夹名只能包含英文字母、数组、汉字和下划线。");
	}
}

// 显示重命名文件夹状态提示
function showRFolderAlert(txt) {
	$("#newfolderalert").addClass("alert");
	$("#newfolderalert").addClass("alert-danger");
	$("#folderrenamebox").addClass("has-error");
	$("#newfolderalert").text(txt);
}

// 显示上传文件模态框
function showUploadFileModel() {
	$("#uploadFileAlert").removeClass("alert");
	$("#uploadFileAlert").removeClass("alert-danger");
	$("#uploadFileAlert").text("");
	if (isUpLoading == false) {
		$("#filepath").removeAttr("disabled");
		$("#uploadfile").val("");
		$("#filepath").val("");
		$("#pros").width("0%");
		$("#pros").attr('aria-valuenow', '0');
		$("#umbutton").attr('disabled', false);
		$("#filecount").text("");
		$("#uploadstatus").text("");
		$("#selectcount").text("");
		$("#selectFileUpLoadModelAsAll").removeAttr("checked");
		$("#selectFileUpLoadModelAlert").hide();
	}
	$('#uploadFileModal').modal('show');
}

// 点击文本框触发input:file选择文件动作
function checkpath() {
	$('#uploadfile').click();
}

// 获取选中文件
function getInputUpload() {
	fs = $("#uploadfile").get(0).files;
	showfilepath();
}

// 文件选中后自动回填文件路径
function showfilepath() {
	var filename = "";
	for (var i = 0; i < fs.length; i++) {
		filename = filename + fs[i].name;
		if (i < (fs.length - 1)) {
			filename = filename + "、";
		}
	}
	if (fs.length <= 1) {
		$("#selectcount").text("");
	} else {
		$("#selectcount").text("（共" + fs.length + "个）");
	}
	$("#filepath").val(filename);
}

// 检查是否能够上传
function checkUploadFile() {
	if (isUpLoading == false) {
		if (fs != null && fs.length > 0) {
			$("#filepath").attr("disabled", "disabled");
			$("#umbutton").attr('disabled', true);
			isUpLoading = true;
			repeModelList = null;
			$("#uploadFileAlert").removeClass("alert");
			$("#uploadFileAlert").removeClass("alert-danger");
			$("#uploadFileAlert").text("");
			var filenames = new Array();
			for (var i = 0; i < fs.length; i++) {
				filenames[i] = fs[i].name.replace(/^.+?\\([^\\]+?)?$/gi, "$1");
			}
			var namelist = JSON.stringify(filenames);

			$.ajax({
				type : "POST",
				dataType : "text",
				data : {
					folderId : locationpath,
					namelist : namelist
				},
				url : "/filemgrctl/checkUploadFile.ajax",
				success : function(result) {
					if (result == "errorParameter") {
						showUploadFileAlert("提示：参数不正确，无法开始上传");
					} else if (result == "noAuthorized") {
						showUploadFileAlert("提示：您的操作未被授权，无法开始上传");
					} else if (result.startsWith("duplicationFileName:")) {
						repeList = eval("(" + result.substring(20) + ")");
						repeIndex = 0;
						selectFileUpLoadModelStart();
					} else if (result == "permitUpload") {
						doupload(1);
					} else {
						showUploadFileAlert("提示：出现意外错误，无法开始上传");
					}
				},
				error : function() {
					showUploadFileAlert("提示：出现意外错误，无法开始上传");
				}
			});
		} else {
			showUploadFileAlert("提示：您未选择任何文件，无法开始上传");
		}
	}
}

var repeList; // 这个是重复文件名的列表，型如['xxx','ooo',...]
var repeIndex; // 当前设定上传模式的文件序号
var repeModelList; // 这个是对每一个重复文件选取的上传模式，型如{'xxx':'skip','ooo':'both',...}

// 针对同名文件，选择上传的模式：跳过（skip）、覆盖（cover）和保留两者（both）
function selectFileUpLoadModelStart() {
	$("#selectFileUpLoadModelAlert").show();
	$("#repeFileName").text(repeList[repeIndex]);
}

function selectFileUpLoadModelEnd(t) {
	if (repeModelList == null) {
		repeModelList = {};
	}
	repeModelList[$("#repeFileName").text()] = t;
	$("#selectFileUpLoadModelAlert").hide();
	if ($('#selectFileUpLoadModelAsAll').prop('checked')) {
		for (var i = repeIndex; i < repeList.length; i++) {
			repeModelList[repeList[i]] = t;
		}
		doupload(1);
	} else {
		repeIndex++;
		if (repeIndex < repeList.length) {
			selectFileUpLoadModelStart();
		} else {
			doupload(1);
		}
	}
}

// 执行文件上传并实现上传进度显示
function doupload(count) {
	var fcount = fs.length;
	$("#pros").width("0%"); // 先将进度条置0
	$("#pros").attr('aria-valuenow', "0");
	var uploadfile = fs[count - 1]; // 获取要上传的文件
	if (uploadfile != null) {
		var fname = uploadfile.name;
		if (fcount > 1) {
			$("#filecount").text("（" + count + "/" + fcount + "）"); // 显示当前进度
		}
		$("#uploadstatus").prepend(
				"<p>" + fname + "<span id='uls_" + count
						+ "'>[正在上传...]</span></p>");
		xhr = new XMLHttpRequest(); // 这东西类似于servlet里面的request

		var fd = new FormData(); // 用于封装文件数据的对象

		fd.append("file", uploadfile); // 将文件对象添加到FormData对象中，字段名为uploadfile
		fd.append("folderId", locationpath);
		if (repeModelList != null && repeModelList[fname] != null) {
			if (repeModelList[fname] == 'skip') {
				$("#uls_" + count).text("[已完成]");
				if (count < fcount) {
					doupload(count + 1);
					return;
				} else {
					// 清空所有提示信息，还原上传窗口
					isUpLoading = false;
					$("#filepath").removeAttr("disabled");
					$("#uploadfile").val("");
					$("#filepath").val("");
					$("#pros").width("0%");
					$("#pros").attr('aria-valuenow', "0");
					$("#umbutton").attr('disabled', false);
					$("#filecount").text("");
					$("#uploadstatus").text("");
					$("#selectcount").text("");
					$('#uploadFileModal').modal('hide');
					showFolderView(locationpath);
					return;
				}
			}
			fd.append("repeType", repeModelList[fname]);
		}
		xhr.open("POST", "/filemgrctl/douploadFile.ajax", true); // 上传目标

		xhr.upload.addEventListener("progress", uploadProgress, false); // 这个是对上传进度的监听
		// 上面的三个参数分别是：事件名（指定名称）、回调函数、是否冒泡（一般是false即可）

		xhr.send(fd); // 上传FormData对象

		// 上传结束后执行的回调函数
		xhr.onloadend = function() {
			if (xhr.status === 200) {
				// TODO 上传成功
				var result = xhr.responseText;
				if (result == "uploadsuccess") {
					$("#uls_" + count).text("[已完成]");
					if (count < fcount) {
						doupload(count + 1);
					} else {
						// 清空所有提示信息，还原上传窗口
						isUpLoading = false;
						$("#filepath").removeAttr("disabled");
						$("#uploadfile").val("");
						$("#filepath").val("");
						$("#pros").width("0%");
						$("#pros").attr('aria-valuenow', "0");
						$("#umbutton").attr('disabled', false);
						$("#filecount").text("");
						$("#uploadstatus").text("");
						$("#selectcount").text("");
						$('#uploadFileModal').modal('hide');
						showFolderView(locationpath);
					}
				} else if (result == "uploaderror") {
					showUploadFileAlert("提示：出现意外错误，文件：[" + fname
							+ "]上传失败，上传被中断。");
					$("#uls_" + count).text("[失败]");
				} else {
					showUploadFileAlert("提示：出现意外错误，文件：[" + fname
							+ "]上传失败，上传被中断。");
					$("#uls_" + count).text("[失败]");
				}
			} else {
				showUploadFileAlert("提示：出现意外错误，文件：[" + fname + "]上传失败，上传被中断。");
				$("#uls_" + count).text("[失败]");
			}
		};
	} else {
		showUploadFileAlert("提示：要上传的文件不存在。");
		$("#uploadstatus").prepend(
				"<p>未找到要上传的文件<span id='uls_" + count + "'>[失败]</span></p>");
	}
}

function uploadProgress(evt) {
	if (evt.lengthComputable) {
		// evt.loaded：文件上传的大小 evt.total：文件总的大小
		var percentComplete = Math.round((evt.loaded) * 100 / evt.total);
		// 加载进度条，同时显示信息
		$("#pros").width(percentComplete + "%");
		$("#pros").attr('aria-valuenow', "" + percentComplete);
	}
}

// 显示上传文件错误提示
function showUploadFileAlert(txt) {
	isUpLoading = false;
	$("#filepath").removeAttr("disabled");
	$("#uploadFileAlert").addClass("alert");
	$("#uploadFileAlert").addClass("alert-danger");
	$("#uploadFileAlert").text(txt);
	$("#umbutton").attr('disabled', false);
}

// 显示下载文件模态框
function showDownloadModel(fileId, fileName) {
	$("#downloadModal").modal('toggle');
	$("#downloadFileName").text("提示：您确认要下载文件：[" + fileName + "]么？");
	$("#downloadHrefBox").html(
			"<a href='" + window.location.protocol + "//"
					+ window.location.host
					+ "//filemgrctl/downloadFile.do?fileId=" + fileId + "'>"
					+ window.location.protocol + "//" + window.location.host
					+ "//filemgrctl/downloadFile.do?fileId=" + fileId + "</a>");
	$("#downloadFileBox")
			.html(
					"<button id='dlmbutton' type='button' class='btn btn-primary' onclick='dodownload("
							+ '"' + fileId + '"' + ")'>开始下载</button>");
	$("#dlmbutton").attr('disabled', false);
}

// 执行下载操作
function dodownload(fileId) {
	$("#dlmbutton").attr('disabled', true);
	$("#downloadFileName").text("提示：准备开始下载，请稍候...");
	var t = setTimeout("$('#downloadModal').modal('hide');", 800);
	window.location.href = "/filemgrctl/downloadFile.do?fileId=" + fileId;
}

// 显示删除文件模态框
function showDeleteFileModel(fileId, fileName) {
	$('#deleteFileBox')
			.html(
					"<button id='dfmbutton' type='button' class='btn btn-danger' onclick='deleteFile("
							+ '"' + fileId + '"' + ")'>删除</button>");
	$("#dfmbutton").attr('disabled', false);
	$('#deleteFileMessage').text("提示：确定要彻底删除文件：[" + fileName + "]么？该操作不可恢复");
	$('#deleteFileModal').modal('toggle');
}

// 执行删除文件操作
function deleteFile(fileId) {
	$("#dfmbutton").attr('disabled', true);
	$('#deleteFileMessage').text("提示：正在删除，请稍候...");
	$.ajax({
		type : "POST",
		dataType : "text",
		data : {
			fileId : fileId
		},
		url : "/filemgrctl/deleteFile.ajax",
		success : function(result) {
			if (result == "mustLogin") {
				window.location.href = "login.html";
			} else {
				if (result == "noAuthorized") {
					$('#deleteFileMessage').text("提示：您的操作未被授权，删除失败");
					$("#dfmbutton").attr('disabled', false);
				} else if (result == "errorParameter") {
					$('#deleteFileMessage').text("提示：参数不正确，删除失败");
					$("#dfmbutton").attr('disabled', false);
				} else if (result == "cannotDeleteFile") {
					$('#deleteFileMessage').text("提示：出现意外错误，可能未能删除文件");
					$("#dfmbutton").attr('disabled', false);
				} else if (result == "deleteFileSuccess") {
					$('#deleteFileModal').modal('hide');
					showFolderView(locationpath);
				} else {
					$('#deleteFileMessage').text("提示：出现意外错误，可能未能删除文件");
					$("#dfmbutton").attr('disabled', false);
				}
			}
		},
		error : function() {
			$('#deleteFileMessage').text("提示：出现意外错误，可能未能删除文件");
			$("#dfmbutton").attr('disabled', false);
		}
	});
}

// 显示重命名文件模态框
function showRenameFileModel(fileId, fileName) {
	$("#newFileNamealert").removeClass("alert");
	$("#newFileNamealert").removeClass("alert-danger");
	$("#filerenamebox").removeClass("has-error");
	$("#newFileNamealert").text("");
	$("#renameFileBox").html(
			"<button type='button' class='btn btn-primary' onclick='renameFile("
					+ '"' + fileId + '"' + ")'>修改</button>");
	$("#newfilename").val(fileName);
	$("#renameFileModal").modal('toggle');
}

// 修改文件名
function renameFile(fileId) {
	var reg = new RegExp("[\/\|\\s\\\\\*\\<\\>" + '"' + "]+", "g");
	var newFileName = $("#newfilename").val();
	if (newFileName.length > 0) {
		if (newFileName.length < 128) {
			if (!reg.test(newFileName) && newFileName.indexOf(".") != 0) {
				$.ajax({
					type : "POST",
					dataType : "text",
					data : {
						fileId : fileId,
						newFileName : newFileName
					},
					url : "/filemgrctl/renameFile.ajax",
					success : function(result) {
						if (result == "mustLogin") {
							window.location.href = "login.html";
						} else {
							if (result == "cannotRenameFile") {
								showRFileAlert("提示：出现意外错误，可能未能重命名文件，请刷新后重试。");
							} else if (result == "renameFileSuccess") {
								$('#renameFileModal').modal('hide');
								showFolderView(locationpath);
							} else if (result == "errorParameter") {
								showRFileAlert("提示：参数错误，重命名失败，请刷新后重试。");
							} else if (result == "nameOccupied") {
								showRFileAlert("提示：该名称已被占用，请选取其他名称。");
							} else if (result == "noAuthorized") {
								showRFileAlert("提示：您的操作未被授权，重命名失败，请刷新后重试。");
							} else {
								showRFileAlert("提示：出现意外错误，可能未能重命名文件，请刷新后重试。");
							}
						}
					},
					error : function() {
						showRFileAlert("提示：出现意外错误，可能未能重命名文件。");
					}
				});
			} else {
				showRFileAlert("提示：文件名中不应含有：空格 引号 / \ * | < > 且不能以“.”开头。");
			}
		} else {
			showRFileAlert("提示：文件名称太长。");
		}
	} else {
		showRFileAlert("提示：文件名不能为空。");
	}
}

// 显示重命名文件状态提示
function showRFileAlert(txt) {
	$("#newFileNamealert").addClass("alert");
	$("#newFileNamealert").addClass("alert-danger");
	$("#filerenamebox").addClass("has-error");
	$("#newFileNamealert").text(txt);
}

// 取消上传
function abortUpload() {
	isUpLoading = false;
	if (xhr != null) {
		xhr.abort();
		$("#umbutton").attr('disabled', false);
		$("#pros").width("0%");
		$("#pros").attr('aria-valuenow', "0");
		$("#filecount").text("");
	}
	$("#uploadfile").val("");
	$("#filepath").val("");
	$("#uploadstatus").html("");
	$("#selectcount").text("");
	$('#uploadFileModal').modal('hide');
	showFolderView(locationpath);
}

// 获取文件名的后缀名，以小写形式输出
function getSuffix(filename) {
	var index1 = filename.lastIndexOf(".");
	var index2 = filename.length;
	var suffix = filename.substring(index1 + 1, index2);
	return suffix.toLowerCase();
}
// 兼容Chrome、IE、FF的Shift判定
function isShift(event) {
	var e = window.event || event;
	if (e.shiftKey) {
		return true;
	} else {
		return false;
	}
}

// 选中某一行文件，如果使用Shift点击则为多选
function checkfile(event, fileId) {
	if (!isShift(event)) {
		$(".filerow").removeClass("info");
		$("#" + fileId).addClass("info");
	} else {
		if ($("#" + fileId).hasClass("info")) {
			$("#" + fileId).removeClass("info");
		} else {
			$("#" + fileId).addClass("info");
		}
	}
}
// 切换全部文件行的选中或非选中
function checkallfile() {
	if ($(".filerow.info").length == $(".filerow").length) {
		$(".filerow").removeClass("info");
	} else {
		$(".filerow").addClass("info");
	}
}
// 连续选中若干行文件：Shift+双击，选中规则为：前有选前，后有选后，全有也选后。
function checkConsFile(event, fileId) {
	if (isShift(event)) {
		var endRow = $("#" + fileId);
		var endRowIndex = endRow.index();
		var startRowIndex = $('.filerow.info:last').index();
		if (startRowIndex != -1) {
			if (startRowIndex < endRowIndex) {
				while (endRow[0] && !endRow.hasClass("info")) {
					endRow.addClass("info");
					endRow = endRow.prev();
				}
			} else {
				while (endRow[0] && !endRow.hasClass("info")) {
					endRow.addClass("info");
					endRow = endRow.next();
				}
			}
		}
	}
}

// 用于获取全部选中的文件夹和文件ID，该function返回一个对象。
// 该对象中，filesId属性为文件ID，foldersId属性为文件夹ID。两个属性均为JSON数组形式的字符串，可直接发送至服务器。此外还有size，包含了元素的总数。
function getCheckedFilesAndFolders() {
	var filesAndFolders = new Object();
	filesAndFolders.size = 0;
	var filesId = new Array();
	var foldersId = new Array();
	var checkedfiles = $(".info").get();
	for (var i = 0; i < checkedfiles.length; i++) {
		if (checkedfiles[i].getAttribute("iskfolder") == "true") {
			foldersId.push(checkedfiles[i].id);
		} else {
			filesId.push(checkedfiles[i].id);
		}
		filesAndFolders.size++;
	}
	filesAndFolders.filesId = JSON.stringify(filesId);
	filesAndFolders.foldersId = JSON.stringify(foldersId);
	return filesAndFolders;
}

// 显示打包下载模态框
function showDownloadAllCheckedModel() {
	$("#downloadAllCheckedBox").html("");
	$("#downloadAllCheckedLoad").text("");
	var faf = getCheckedFilesAndFolders();
	if (faf.size == 0) {
		$("#downloadAllCheckedName").html(checkFilesTip);
	} else {
		$("#downloadAllCheckedName").text("提示：您确认要打包并下载这" + faf.size + "项么？");
		$("#downloadAllCheckedBox")
				.html(
						"<button id='dclmbutton' type='button' class='btn btn-primary' onclick='downloadAllChecked()'>开始下载</button>");
		$("#dclmbutton").attr('disabled', false);
	}
	$("#downloadAllCheckedModal").modal('toggle');
}

// 下载选中的所有文件
function downloadAllChecked() {
	$("#dclmbutton").attr('disabled', true);
	var faf = getCheckedFilesAndFolders();
	$("#downloadAllCheckedName").text(
			"提示：服务器正在对选中资源进行压缩（共" + faf.size
					+ "项），这可能需要一些时间（文件越大耗时越长），压缩完成将自动开始下载。");
	// 计算预计耗时
	$.ajax({
		url : '/filemgrctl/getPackTime.ajax',
		type : 'POST',
		data : {
			strIdList : faf.filesId,
			strFidList : faf.foldersId
		},
		dataType : 'text',
		success : function(result) {
			if (result != "0") {
				var count = 0;
				$("#downloadAllCheckedLoad").text(
						"已耗时：" + count + "秒（预计耗时：" + result + "）");
				zipTimer = setInterval(function() {
					count++;
					$("#downloadAllCheckedLoad").text(
							"已耗时：" + count + "秒（预计耗时：" + result + "）");
				}, 1000);
			} else {
				var count = 0;
				$("#downloadAllCheckedLoad").text("已耗时：" + count + "秒");
				zipTimer = setInterval(function() {
					count++;
					$("#downloadAllCheckedLoad").text("已耗时：" + count + "秒");
				}, 1000);
			}
		},
		error : function() {
			$("#downloadAllCheckedLoad").text("（无法获取预计耗时）");
		}
	});
	// 同时发送压缩下载请求
	$.ajax({
		type : "POST",
		url : "/filemgrctl/downloadCheckedFiles.ajax",
		data : {
			strIdList : faf.filesId,
			strFidList : faf.foldersId
		},
		dataType : "text",
		success : function(result) {
			if (zipTimer != null) {
				window.clearInterval(zipTimer);
			}
			if (result == "ERROR") {
				$("#downloadAllCheckedName")
						.text("提示：压缩过程出错。无法完成压缩，请重试或告知管理员。");
			} else {
				$("#downloadAllCheckedLoad").text("");
				$("#downloadAllCheckedName").text("提示：压缩完成！准备开始下载...");
				var t = setTimeout(
						"$('#downloadAllCheckedModal').modal('hide');", 800);
				// POST提交全部下载请求
				var temp = document.createElement("form");
				temp.action = '/filemgrctl/downloadCheckedFilesZip.do';
				temp.method = "post";
				temp.style.display = "none";
				var sl = document.createElement("input");
				sl.name = 'zipId';
				sl.value = result;
				temp.appendChild(sl);
				document.body.appendChild(temp);
				temp.submit();
			}
		},
		error : function() {
			$("#downloadAllCheckedName").text("提示：请求失败。无法完成压缩，请重试或告知管理员。");
		}
	});
}

// 删除选中的所有文件
function showDeleteAllCheckedModel() {
	$('#deleteFileBox').html("");
	var faf = getCheckedFilesAndFolders();
	$("#dfmbutton").attr('disabled', false);
	if (faf.size == 0) {
		$('#deleteFileMessage').html(checkFilesTip);
	} else {
		$('#deleteFileBox')
				.html(
						"<button id='dfmbutton' type='button' class='btn btn-danger' onclick='deleteAllChecked()'>全部删除</button>");
		$('#deleteFileMessage').text("提示：确定要彻底删除这" + faf.size + "项么？该操作不可恢复！");
	}
	$('#deleteFileModal').modal('toggle');
}

// 删除选中的所有文件
function deleteAllChecked() {
	// TODO 提交全部删除请求
	var faf = getCheckedFilesAndFolders();
	$("#dfmbutton").attr('disabled', true);
	$('#deleteFileMessage').text("提示：正在删除，请稍候...");
	$.ajax({
		type : "POST",
		dataType : "text",
		data : {
			strIdList : faf.filesId,
			strFidList : faf.foldersId
		},
		url : "/filemgrctl/deleteCheckedFiles.ajax",
		success : function(result) {
			if (result == "mustLogin") {
				window.location.href = "login.html";
			} else {
				if (result == "noAuthorized") {
					$('#deleteFileMessage').text("提示：您的操作未被授权，删除失败");
					$("#dfmbutton").attr('disabled', false);
				} else if (result == "errorParameter") {
					$('#deleteFileMessage').text("提示：参数不正确，未能全部删除文件");
					$("#dfmbutton").attr('disabled', false);
				} else if (result == "cannotDeleteFile") {
					$('#deleteFileMessage').text("提示：出现意外错误，可能未能删除全部文件");
					$("#dfmbutton").attr('disabled', false);
				} else if (result == "deleteFileSuccess") {
					$('#deleteFileModal').modal('hide');
					showFolderView(locationpath);
				} else {
					$('#deleteFileMessage').text("提示：出现意外错误，可能未能删除全部文件");
					$("#dfmbutton").attr('disabled', false);
				}
			}
		},
		error : function() {
			$('#deleteFileMessage').text("提示：出现意外错误，可能未能删除全部文件");
			$("#dfmbutton").attr('disabled', false);
		}
	});
}